<template>
  <rx-layout>
    <div slot="center">
      <a-tree
        :defaultExpandAll="true"
        :show-icon="true"
        :show-line="true"
        :treeData="relationData"
        @rightClick="rightClick"
      >
        <a-icon slot="team" type="team"/>
        <a-icon slot="smile" type="user"/>
      </a-tree>
      <v-contextmenu ref="contextmenu">
        <v-contextmenu-item @click="addRelation">{{i18n('list.addRelation','添加关系')}}</v-contextmenu-item>
        <v-contextmenu-item v-if="isTreeCat" @click="changeRelation">{{i18n('list.changeRel','更换关系')}}</v-contextmenu-item>
        <v-contextmenu-item v-if="isTreeCat" @click="setModal1Visible(true)">{{i18n('list.transferRel','转移关系')}}</v-contextmenu-item>
        <v-contextmenu-item v-if="isTreeCat" @click="delRelation">{{i18n('list.delRelation','删除关系')}}</v-contextmenu-item>
      </v-contextmenu>
      <a-modal
        :zIndex="99999"
        :title="i18n('list.transferRel','转移关系')"
        style="top: 30%;"
        :visible="modal1Visible"
        @ok="() => transferOk()"
        @cancel="() => setModal1Visible(false)"
      >
        <a-tree
          :defaultExpandAll="true"
          :show-icon="true"
          :show-line="true"
          :treeData="relationData"
          @select="selectTransfer"
        >
          <a-icon slot="team" type="team"/>
          <a-icon slot="smile" type="user"/>
        </a-tree>
      </a-modal>
    </div>
  </rx-layout>
</template>

<script>
  import {RxLayout, RxDialog, Util, Dialog} from "@lowcode/jpaas-common-lib"
  import OsRelInstApi from '@/api/user/org/osRelInst'

  export default {
    name: "rel-definition",
    props: ["layerid", "destroy", "relation"],
    components: {
      RxDialog,
      RxLayout
    },
    data() {
      return {
        visible: false,
        isTreeCat: true,
        relType: this.relation.relType,
        relationData: [],
        selectInstId: "",
        modal1Visible:false,
        transferRelInstId:"",
      }
    },
    created() {
      //TreeCat
      this.relationData = [
        {
          key: this.relation.id,
          title: this.relation.name,
          children: [],
          slots: {icon: 'team'}
        }];
      this.initTreeData();
    },
    methods: {
      i18n(name,text,key){
            return this.$ti18(name,text,"osRelType",key);
      },
      initTreeData() {
        OsRelInstApi.getTreeData(this.relation.id).then(res => {
            this.relationData[0].children=this.convertData(res);
          }
        );
      },
      convertData(data){
        var array=[];
        for (var i = 0; i < data.length; i++) {
          var obj = {
            key: data[i].instId,
            title: data[i].fullname,
            children: [],
            slots: {icon: 'smile'}
          };
          if(data[i].children.length>0){
            obj.children=this.convertData(data[i].children);
          }
          array.push(obj);
        }
        return array;
      },
      addRelation() {
        var self = this;
        var conf = {curVm: this, data: {single: false}, widthHeight: ['1000px', '700px']};
        Dialog.openUserDialog(conf, function (self, users) {
          var userIds = [];
          if (users.length > 0) {
            for (var i = 0; i < users.length; i++) {
              userIds.push(users[i].userId);
            }
            OsRelInstApi.addRelInsts(self.relation, userIds, self.selectInstId).then(res => {
              if(res.success){
                self.initTreeData();
              }
            });
          }
        });
      },
      changeRelation() {
        var self = this;
        var conf = {curVm: this, data: {single: true}, widthHeight: ['1000px', '700px']};
        Dialog.openUserDialog(conf, function (self, user) {
          if(user.userId==self.selectInstId){
            self.$notification["warning"]({
              message: self.$ti18('rxMsa.actionTips','操作提示'),
              description:
                  self.i18n('form.node','目标节点不能为当前节点!'),
            });
            return;
          }
          if(user){
            OsRelInstApi.changeRelation(user.userId, self.selectInstId).then(res => {
              if(res.success){
                self.initTreeData();
              }
            });
          }
        });
      },
      delRelation() {
        var self = this;
        if(self.selectInstId!=""){
          this.$confirm({
            title: self.$ti18('rxMsa.actionTips','操作提示'),
            zIndex: 20000,
            content: self.i18n('form.nodeDelete', '您确定需要删除选中的节点吗？'),
              okText: self.$ti18('rxForm.ackBtn','确认'),
              cancelText: self.$ti18('rxForm.cancelBtn','取消'),
            onOk() {
              OsRelInstApi.delRelInsts(self.selectInstId).then(res => {
                if(res.success){
                  self.initTreeData();
                }
              })
            },
            onCancel() {}
          })
        }
      },
      rightClick({event, node}) {
        if (node.dataRef.key == this.relation.id) {
          this.isTreeCat = false;
          this.selectInstId="";
        } else {
          this.isTreeCat = true;
          this.selectInstId = node.dataRef.key;
        }
        const postition = {top: event.clientY, left: event.clientX};
        this.$refs.contextmenu.show(postition);
      },
      onOk() {
        Util.closeWindow(this, "ok");
      },
      cancel() {
        Util.closeWindow(this, "cancel");
      },
      setModal1Visible(modal1Visible){
        this.modal1Visible=modal1Visible;
      },
      selectTransfer(selectedKeys,e){
        this.transferRelInstId=selectedKeys[0];
      },
      transferOk(){
        this.modal1Visible=false;
        if(this.transferRelInstId){
          //目标节点是否为主节点
          if(this.relation.id==this.transferRelInstId){
            this.transferRelInstId="";
          }
          if(this.transferRelInstId==this.selectInstId){
            this.$notification["warning"]({
              message: this.$ti18('rxMsa.actionTips','操作提示'),
              description:
                  this.i18n('form.node','目标节点不能为当前节点!'),
            });
            return;
          }
          OsRelInstApi.transferRelation(this.transferRelInstId, this.selectInstId).then(res => {
            if(res.success){
              this.initTreeData();
            }
          });
        }
      }
    }
  }
</script>

<style scoped>

</style>